package com.ruoyi.mentor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.mentor.bo.RoundAddBo;
import com.ruoyi.mentor.bo.RoundEditBo;
import com.ruoyi.mentor.bo.RoundQueryBo;
import com.ruoyi.mentor.domain.Round;
import com.ruoyi.mentor.mapper.RoundMapper;
import com.ruoyi.mentor.service.IRoundService;
import com.ruoyi.mentor.vo.RoundVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 志愿征集轮次信息Service业务层处理
 *
 * @author LH
 * @date 2021-05-21
 */
@Service
public class RoundServiceImpl extends ServiceImpl<RoundMapper, Round> implements IRoundService {

    @Override
    public RoundVo queryById(Long id){
        return getVoById(id, RoundVo.class);
    }

    @Override
    public TableDataInfo<RoundVo> queryPageList(RoundQueryBo bo) {
        PagePlus<Round, RoundVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), RoundVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<RoundVo> queryList(RoundQueryBo bo) {
        return listVo(buildQueryWrapper(bo), RoundVo.class);
    }

    private LambdaQueryWrapper<Round> buildQueryWrapper(RoundQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Round> lqw = Wrappers.lambdaQuery();
        lqw.ge(bo.getStuStart() != null, Round::getStuStart, bo.getStuStart());
        lqw.le(bo.getStuEnd() != null, Round::getStuEnd, bo.getStuEnd());
        lqw.ge(bo.getInsStart() != null, Round::getInsStart, bo.getInsStart());
        lqw.le(bo.getInsEnd() != null, Round::getInsEnd, bo.getInsEnd());
        lqw.orderByDesc(Round::getStuStart);
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(RoundAddBo bo) {
        Round add = BeanUtil.toBean(bo, Round.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(RoundEditBo bo) {
        Round update = BeanUtil.toBean(bo, Round.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Round entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    /**
     * 查询当前时间是否处于可选时间段
     *
     * @param type 0 学生 ，1导师
     * @return
     */
    @Override
    public RoundVo isSelectTime(Integer type) {
        //获取激活的选择时间
        Round round = getOne(new QueryWrapper<Round>().eq("is_active",1).last("limit 1"));
        if (round==null){
            return null;
        }
        RoundVo roundVo = new RoundVo();
        BeanUtils.copyProperties(round,roundVo);
        if (type==0){
            //学生
            if (round.getStuStart().before(new Date())&&round.getStuEnd().after(new Date())){
                //处于选择时间段
                roundVo.setIsSelect(true);

            }else {
                roundVo.setIsSelect(false);
            }
        }else {
            //导师
            if (round.getInsStart().before(new Date())&&round.getInsEnd().after(new Date())){
                //处于选择时间段
                roundVo.setIsSelect(true);
            }else {
                roundVo.setIsSelect(false);
            }
        }
        return roundVo;
    }

    @Override
    @Transactional
    public void active(Long id) {
        baseMapper.noActive();
        Round round = new Round();
        round.setId(id);
        round.setIsActive(1);
        updateById(round);
    }

    /**
     * 取消激活
     */
    @Override
    public void noActive() {
        baseMapper.noActive();
    }
}
